//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AbcdFunction.h"
using namespace Cephei::QL::Termstructures::Volatility;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ b, Microsoft::FSharp::Core::FSharpOption<Double>^ c, Microsoft::FSharp::Core::FSharpOption<Double>^ d) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phAbcdFunction = NULL;
#endif
        QuantLib::Real _a = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (a) ? (QuantLib::Real)ValueHelper::Convert (a->Value) : -0.06); //4
        QuantLib::Real _b = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (b) ? (QuantLib::Real)ValueHelper::Convert (b->Value) : 0.17); //4
        QuantLib::Real _c = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (c) ? (QuantLib::Real)ValueHelper::Convert (c->Value) : 0.54); //4
        QuantLib::Real _d = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (d) ? (QuantLib::Real)ValueHelper::Convert (d->Value) : 0.17); //4
        _ppAbcdFunction = new boost::shared_ptr<QuantLib::AbcdFunction> (new QuantLib::AbcdFunction ( _a,  _b,  _c,  _d ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (boost::shared_ptr<QuantLib::AbcdFunction>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcdFunction = NULL;
#endif
	_ppAbcdFunction = &childNative;
    
}
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (QuantLib::AbcdFunction& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAbcdFunction = NULL;
#endif
	_ppAbcdFunction = new boost::shared_ptr<QuantLib::AbcdFunction> (&childNative);
    
    _AbcdFunctionOwner = owner;
    
}

Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (CAbcdFunction^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcdFunction = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAbcdFunction = new boost::shared_ptr<QuantLib::AbcdFunction> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcdFunction = NULL;
#endif
	if (!t->IsSubclassOf(CAbcdFunction::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (QuantLib::Handle<QuantLib::AbcdFunction>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAbcdFunction = &childNative;
	_ppAbcdFunction = &static_cast<boost::shared_ptr<QuantLib::AbcdFunction>>(childNative.currentLink());
    
    _AbcdFunctionOwner = owner;
}
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (QuantLib::Handle<QuantLib::AbcdFunction> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAbcdFunction = &childNative;
	_ppAbcdFunction = &static_cast<boost::shared_ptr<QuantLib::AbcdFunction>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::CAbcdFunction::CAbcdFunction (QuantLib::AbcdFunction childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAbcdFunction = NULL;
#endif
	_ppAbcdFunction = new boost::shared_ptr<QuantLib::AbcdFunction> (new QuantLib::AbcdFunction (childNative));
    
}
#endif

Cephei::QL::Termstructures::Volatility::CAbcdFunction::~CAbcdFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAbcdFunction != NULL)
    {
	    delete _ppAbcdFunction;
        _ppAbcdFunction = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::CAbcdFunction::!CAbcdFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAbcdFunction != NULL)
    {
	    delete _ppAbcdFunction;
    }
}
QuantLib::AbcdFunction& Cephei::QL::Termstructures::Volatility::CAbcdFunction::GetReference ()
{
    if (_ppAbcdFunction == NULL) throw gcnew NativeNullException ();
	return **_ppAbcdFunction;
}
boost::shared_ptr<QuantLib::AbcdFunction>& Cephei::QL::Termstructures::Volatility::CAbcdFunction::GetShared ()
{
    if (_ppAbcdFunction == NULL) throw gcnew NativeNullException ();
	return *_ppAbcdFunction;
}
QuantLib::AbcdFunction* Cephei::QL::Termstructures::Volatility::CAbcdFunction::GetPointer ()
{
    if (_ppAbcdFunction == NULL) throw gcnew NativeNullException ();
	return &**_ppAbcdFunction;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AbcdFunction>& Cephei::QL::Termstructures::Volatility::CAbcdFunction::GetHandle ()
{
	if (_phAbcdFunction == NULL)
	{
		_phAbcdFunction = new Handle<QuantLib::AbcdFunction> (*_ppAbcdFunction);
	}
	return *_phAbcdFunction;
}
#endif
bool Cephei::QL::Termstructures::Volatility::CAbcdFunction::HasNative () 
{
	return (_ppAbcdFunction != NULL);
}

Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::A::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->a ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::B::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->b ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::C::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->c ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::Covariance (Double t1, Double t2, Double T, Double S)
{
    try
    {
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
        QuantLib::Time _T = (QuantLib::Time)ValueHelper::Convert (T);
        QuantLib::Time _S = (QuantLib::Time)ValueHelper::Convert (S);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->covariance ( _t1,  _t2,  _T,  _S );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::Covariance (Double t, Double T2, Double S)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
        QuantLib::Time _S = (QuantLib::Time)ValueHelper::Convert (S);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->covariance ( _t,  _T2,  _S );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::D::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->d ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::InstantaneousCovariance (Double u, Double T, Double S)
{
    try
    {
        QuantLib::Time _u = (QuantLib::Time)ValueHelper::Convert (u);
        QuantLib::Time _T = (QuantLib::Time)ValueHelper::Convert (T);
        QuantLib::Time _S = (QuantLib::Time)ValueHelper::Convert (S);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->instantaneousCovariance ( _u,  _T,  _S );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::InstantaneousVariance (Double t, Double T2)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->instantaneousVariance ( _t,  _T2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::InstantaneousVolatility (Double t, Double T2)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->instantaneousVolatility ( _t,  _T2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::LongTermVolatility::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->longTermVolatility ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::MaximumLocation::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->maximumLocation ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::MaximumVolatility::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->maximumVolatility ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::Primitive (Double t, Double T2, Double S)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
        QuantLib::Time _S = (QuantLib::Time)ValueHelper::Convert (S);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->primitive ( _t,  _T2,  _S );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::ShortTermVolatility::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->shortTermVolatility ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::Variance (Double tMin, Double tMax, Double T)
{
    try
    {
        QuantLib::Time _tMin = (QuantLib::Time)ValueHelper::Convert (tMin);
        QuantLib::Time _tMax = (QuantLib::Time)ValueHelper::Convert (tMax);
        QuantLib::Time _T = (QuantLib::Time)ValueHelper::Convert (T);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->variance ( _tMin,  _tMax,  _T );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CAbcdFunction::Volatility (Double tMin, Double tMax, Double T)
{
    try
    {
        QuantLib::Time _tMin = (QuantLib::Time)ValueHelper::Convert (tMin);
        QuantLib::Time _tMax = (QuantLib::Time)ValueHelper::Convert (tMax);
        QuantLib::Time _T = (QuantLib::Time)ValueHelper::Convert (T);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAbcdFunction)->volatility ( _tMin,  _tMax,  _T );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::IAbcdFunction^ Cephei::QL::Termstructures::Volatility::CAbcdFunction_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ b, Microsoft::FSharp::Core::FSharpOption<Double>^ c, Microsoft::FSharp::Core::FSharpOption<Double>^ d)
{
    return gcnew CAbcdFunction ( a,  b,  c,  d);
}
